package cn.tedu.cinema.employee.service.impl;

import cn.tedu.cinema.base.exception.ServiceException;
import cn.tedu.cinema.base.response.JsonResult;
import cn.tedu.cinema.base.response.StatusCode;
import cn.tedu.cinema.employee.mapper.EmployeeMapper;
import cn.tedu.cinema.employee.pojo.dto.EmployeeLoginParam;
import cn.tedu.cinema.employee.pojo.dto.EmployeeQuery;
import cn.tedu.cinema.employee.pojo.dto.EmployeeSaveParam;
import cn.tedu.cinema.employee.pojo.entity.Employee;
import cn.tedu.cinema.employee.pojo.vo.EmployeeVO;
import cn.tedu.cinema.employee.service.EmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/* 业务层实现类需要添加@Service注解,表示该类为业务层实现类 */
@Slf4j
@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    EmployeeMapper employeeMapper;

    @Override
    public EmployeeVO login(EmployeeLoginParam employeeLoginParam) {
        log.debug("登录业务,参数:{}", employeeLoginParam);
        //根据前端传来的用户名查询数据库中是否有此用户的数据 Alt+Enter
        EmployeeVO employeeVO = employeeMapper.selectByEmployeeName(employeeLoginParam.getEmployeeName());
        /* 如果业务层遇到了问题,不可以直接给前端响应,要抛出异常,由全局异常处理器来处理! */
        if(employeeVO == null){
            throw new ServiceException(StatusCode.USERNAME_ERROR);
        }
        if(!employeeVO.getPassword().equals(employeeLoginParam.getPassword())){
            throw new ServiceException(StatusCode.PASSWORD_ERROR);
        }
        /* 如果没有抛出异常,业务层会将正常处理完毕的employeeVO结果返回给Controller层
        再由Controller层响应给前端 */
        return employeeVO;
    }

    @Override
    public void saveEmployee(EmployeeSaveParam employeeSaveParam) {
        log.debug("保存用户业务,参数:{}", employeeSaveParam);
        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeSaveParam,employee);
        //需要判断是新增还是修改操作
        if(employee.getId()==null){
            employee.setPassword("123456");//员工不是自己注册,所以需要设置初始密码
            employee.setCreateTime(new Date());//设置创建时间为当前时间
            employeeMapper.insert(employee);//调用数据层新增员工
        }else{
            //TODO 更新用户
            employee.setUpdateTime(new Date());
            employeeMapper.update(employee);
        }
    }

    @Override
    public PageInfo<EmployeeVO> selectEmployee(int pageNum, int pageSize, EmployeeQuery employeeQuery) {
        log.debug("查询用户业务,参数:{}", employeeQuery);

        List<EmployeeVO> list = employeeMapper.selectEmployee(pageNum,pageSize,employeeQuery);
        PageInfo<EmployeeVO> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public void resetPassword(Long employeeId) {
        log.debug("重置密码业务,参数:{}",employeeId);
        Employee employee = new Employee();
        employee.setId(employeeId);
        employee.setPassword("root");
        employee.setUpdateTime(new Date());
        employeeMapper.update(employee);
    }

    @Override
    public void updateStatus(Long employeeId, String status) {
        log.debug("修改用户状态业务,参数:{},{}",employeeId,status);
        Employee employee = new Employee();
        employee.setId(employeeId);
        employee.setStatus(status);
        employee.setUpdateTime(new Date());
        employeeMapper.update(employee);
    }

    @Override
    public void deleteEmployee(Long employeeId) {
        log.debug("删除用户业务,参数:{}",employeeId);
        employeeMapper.deleteById(employeeId);
    }

    @Override
    public EmployeeVO selectById(Long id) {
        log.debug("根据id查询用户业务,参数:{}",id);
        EmployeeVO list = employeeMapper.selectById(id);
        return list;
    }


}







